# 导包
import requests
import unittest
from api.loginApi import LoginApi


# 创建测试用例的类
class TestLoginApi(unittest.TestCase):

    # 登陆成功（正确的用户名和密码）
    def test01_login_success(self):
        login_api = LoginApi()
        json_dict = {"mobile": "13800000001", "password": "123456"}
        res = login_api.login(json_dict)
        res_json = res.json()
        print(res_json)
        # 断言
        self.assertEqual(200, res.status_code)
        self.assertEqual(True, res_json.get('success'))
        self.assertEqual(10000, res_json.get('code'))
        self.assertIn("成功", res_json.get('message'))

    # 登录失败（不传请求体）
    def test02_login_no_boby(self):
        login_api = LoginApi()
        json_dict = None
        res = login_api.login(json_dict)
        res_json = res.json()
        print(res_json)
        # 断言
        self.assertEqual(200, res.status_code)
        self.assertEqual(False, res_json.get('success'))
        self.assertEqual(99999, res_json.get('code'))
        self.assertIn("系统繁忙", res_json.get('message'))

    # 登录失败（不传用户名）
    def test03_login_username_None(self):
        login_api = LoginApi()
        json_dict = {"password": "123456"}
        res = login_api.login(json_dict)
        res_json = res.json()
        print(res_json)
        # 断言
        self.assertEqual(200, res.status_code)
        self.assertEqual(False, res_json.get('success'))
        self.assertEqual(20001, res_json.get('code'))
        self.assertIn("错误", res_json.get('message'))

    # 登录失败（不传用户名密码参数）
    def test04_login_None(self):
        login_api = LoginApi()
        json_dict = {}
        res = login_api.login(json_dict)
        res_json = res.json()
        print(res_json)
        # 断言
        self.assertEqual(200, res.status_code)
        self.assertEqual(False, res_json.get('success'))
        self.assertEqual(20001, res_json.get('code'))
        self.assertIn("错误", res_json.get('message'))

    # 登录失败（不传密码参数）
    def test05_login_password_None(self):
        login_api = LoginApi()
        json_dict = {"mobile":"13800000001"}
        res = login_api.login(json_dict)
        res_json = res.json()
        print(res_json)
        # 断言
        self.assertEqual(200, res.status_code)
        self.assertEqual(False, res_json.get('success'))
        self.assertEqual(20001, res_json.get('code'))
        self.assertIn("错误", res_json.get('message'))

    # 登录失败（用户名为空）
    def test06_login_username_not(self):
        login_api = LoginApi()
        json_dict = {"mobile":"","password":"123456"}
        res = login_api.login(json_dict)
        res_json = res.json()
        print(res_json)
        # 断言
        self.assertEqual(200, res.status_code)
        self.assertEqual(False, res_json.get('success'))
        self.assertEqual(20001, res_json.get('code'))
        self.assertIn("错误", res_json.get('message'))

    # 登录失败（密码为空）
    def test07_login_password_not(self):
        login_api = LoginApi()
        json_dict = {"mobile":"13800000001","password":""}
        res = login_api.login(json_dict)
        res_json = res.json()
        print(res_json)
        # 断言
        self.assertEqual(200, res.status_code)
        self.assertEqual(False, res_json.get('success'))
        self.assertEqual(20001, res_json.get('code'))
        self.assertIn("错误", res_json.get('message'))

    # 登录失败（用户名密码都为空）
    def test08_login_suername_password_not(self):
        login_api = LoginApi()
        json_dict = {"mobile":"","password":""}
        res = login_api.login(json_dict)
        res_json = res.json()
        print(res_json)
        # 断言
        self.assertEqual(200, res.status_code)
        self.assertEqual(False, res_json.get('success'))
        self.assertEqual(20001, res_json.get('code'))
        self.assertIn("错误", res_json.get('message'))

    # 登录失败（用户名为整数）
    def test09_login_username_int(self):
        login_api = LoginApi()
        json_dict = {"mobile":13800000001,"password":"123456"}
        res = login_api.login(json_dict)
        res_json = res.json()
        print(res_json)
        # 断言
        self.assertEqual(200, res.status_code)
        self.assertEqual(False, res_json.get('success'))
        self.assertEqual(20001, res_json.get('code'))
        self.assertIn("错误", res_json.get('message'))

    # 登录失败（密码为整数）
    def test10_login_password_int(self):
        login_api = LoginApi()
        json_dict = {"mobile":"13800000001","password":123456}
        res = login_api.login(json_dict)
        res_json = res.json()
        print(res_json)
        # 断言
        self.assertEqual(200, res.status_code)
        self.assertEqual(False, res_json.get('success'))
        self.assertEqual(20001, res_json.get('code'))
        self.assertIn("错误", res_json.get('message'))

    # 手机号不存在（错误用户名）
    def test11_login_username_False(self):
        login_api = LoginApi()
        json_dict = {"mobile":"13897894561","password":"123456"}
        res = login_api.login(json_dict)
        res_json = res.json()
        print(res_json)
        # 断言
        self.assertEqual(200, res.status_code)
        self.assertEqual(False, res_json.get('success'))
        self.assertEqual(20001, res_json.get('code'))
        self.assertIn("错误", res_json.get('message'))

    # 手机号存在（密码错误）
    def test12_login_password_False(self):
        login_api = LoginApi()
        json_dict = {"mobile":"13800000001","password":"12356"}
        res = login_api.login(json_dict)
        res_json = res.json()
        print(res_json)
        # 断言
        self.assertEqual(200, res.status_code)
        self.assertEqual(False, res_json.get('success'))
        self.assertEqual(20001, res_json.get('code'))
        self.assertIn("错误", res_json.get('message'))

